package com.iwanghang.sqlitedemo;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.iwanghang.sqlitedemo.dialog.UpdateDialog;
import com.iwanghang.sqlitedemo.event.ListChangedEvent;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.SoftReference;
import java.util.List;

import static com.iwanghang.sqlitedemo.MainActivity.UPDATE_REQUESTCODE;

public class ListAdapter extends BaseAdapter {
    private List<Person> list;
    private LayoutInflater inflater;
    private SoftReference<Context> context;
    private SoftReference<SQLiteHelper> sqliteHelper;

    public ListAdapter(List<Person> list, Context context, SQLiteHelper db) {
        this.list = list;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = new SoftReference<>(context);
        this.sqliteHelper = new SoftReference<>(db);
    }

    @Override
    public int getCount() {
        return list.size();
    }


    public void setList(List<Person> list) {
        this.list = list;
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.activity_main_list1, null);
        TextView idView = (TextView) convertView.findViewById(R.id.text_id);
        TextView nameView = (TextView) convertView.findViewById(R.id.text_name);
        TextView ageView = (TextView) convertView.findViewById(R.id.text_age);
        TextView sexView = (TextView) convertView.findViewById(R.id.text_sex);
        Button delete = (Button) convertView.findViewById(R.id.list_delete);
        Button update = (Button) convertView.findViewById(R.id.list_update);
        final Person person = list.get(position);
        idView.setText(String.valueOf(person.get_id()));
        nameView.setText(person.getName());
        ageView.setText(String.valueOf(person.getAge()));
        sexView.setText(String.valueOf(person.getSex()));
        //
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getDB() != null) {
                    int id = person.get_id();
                    if (id != -1) {
                        if (getDB().queryPersonById(id) != null) {
                            getDB().deletePerson(id);
                            showMessage("删除成功!");
                            EventBus.getDefault().post(new ListChangedEvent());
                        } else {
                            showMessage("删除失败\t_id:" + id + "不存在!");
                        }
                    } else {
                        showMessage("删除失败!");
                    }
                }
            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), UpdateDialog.class);
                intent.putExtra("personId", person.get_id());
                intent.putExtra("personName", person.getName());
                intent.putExtra("personAge", person.getAge());
                intent.putExtra("personSex", person.getSex());
                //intent.putExtra("personSexid", person.getSexid());
                if (getContext() instanceof MainActivity) {
                    ((MainActivity) getContext()).startActivityForResult(intent, UPDATE_REQUESTCODE);
                }
            }
        });
        return convertView;
    }

    private void showMessage(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Nullable
    private Context getContext() {
        return context == null ? null : context.get();
    }

    @Nullable
    private SQLiteHelper getDB() {
        return sqliteHelper == null ? null : sqliteHelper.get();
    }
}
